// pages/user/shop-after-list/index.js
const app = getApp()
const api  = require('../../../api/user')
import {initLoadMoreMixin} from '../../../utils/list-load-more'
import Toast from '../../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
import Dialog from '../../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'
const  workSTATUS_TEXT= {
    '-1': "关闭工单",
    0: "未处理",
    1: "客服通过移交财务处理",
    2: "财务打款",
    25: "部分发货",
    3: "已收货",
    4: "待退款",
    5: "售后中",
    6: "已完成",
}
const STATUS_TEXT= {
    '-1': "已取消",
    0: "待付款",
    1: "待发货",
    2: "已发货",
    25: "部分发货",
    3: "已收货",
    4: "待退款",
    5: "售后中",
    6: "已完成",
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    tabsItem: [
        {
        name: "全部",
        status: 0,
        },
        {
        name: "申请售后",
        status: 1,
        },
        {
        name: "售后中",
        status: 5,
        },
        {
        name: "已完成",
        status: 6,
        }
    ],
    getStatusText: {
        1: STATUS_TEXT,
        5: STATUS_TEXT,
        0: workSTATUS_TEXT,
        6: workSTATUS_TEXT
    },
   
    searchValue: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    initLoadMoreMixin.call(this)
    const teamId = options.id
    this.setData({teamId})
    this.resetInit()
  },
  initData() {
    const i = this.data.activeIndex;
    const status = this.data.tabsItem[i].status;
    return app.requestUrl({
        url: '/wxapi/order/worklist', 
        data: {
            limit: this.data.pages.perPage,
            offset: (this.data.pages.currPage - 1) * this.data.pages.perPage,
            tmid: this.data.teamId,
            status: status,
            queryItem: this.data.searchValue
    }}).then(res => {
        /// 异常情况可以统一拦截
     
      const {
        rows,
        total
      } = res.data.data;
      
      // this.items=rows;
      console.log('rows',rows)
      this.setData({
          items: this.data.items.concat(rows)
      })
      return {
        pageCount: rows.length,
        totalCount: total
      };
    })
  },
  handleTabClick(e) {
    this.setData({activeIndex: e.detail.index})
    // this.activeIndex = index;
    this.resetInit();
  },
  
  toOrderDetail(e) {
    const i = e.currentTarget.dataset.index
    const orderId = this.data.items[i].id;

    wx.navigateTo({url: `/pages/user/order-info/order-info?payId=${orderId}`}) //order/payment/:payId
    // this.$router.push({
    //   name: "payment",
    //   params: {
    //     payId: orderId
    //   }
    // })
  },

  onSearch() {
    this.resetInit();
  },
  onChange(e) {
    this.setData({
        searchValue: e.detail
    });
  },
  addWorkOrder(e) {
    const goods = e.currentTarget.dataset.goods
    /// :todo
    const search = `?orderId=${goods.orderid}&goodsId=${goods.id}&goodsPrice=${goods.marketprice}&goodsNum=${goods.total}`
    wx.navigateTo({
      url:'/sub/user/shop-order-list/add-work-order'+ search
    })
    // this.$router.push({
    //   name: "add-work-order",
    //   query: {
    //     orderId: goods.orderid,
    //     goodsId: goods.id,
    //     goodsPrice: goods.marketprice,
    //     goodsNum: goods.total
    //   }
    // });
  },
  cancelWorkOrder(e) {
    const goods = e.currentTarget.dataset.goods
    Dialog.confirm({
          message: "是否确认撤销售后申请",
          cancelButtonText: "取消"
        }).then(() => {
          app.requestUrl({url: api.WORKORDER_CANCEL, data:{id: goods.workId}}).then(res => {
            if (res.data.data === 1) {
                Toast.success('工单撤销成功');
                this.resetInit();
            }
          });
        });
  },
  workOrder(e) {
    console.log(e)
   
    const goods = e.currentTarget.dataset.goods
    const workId = goods.workId
    console.log(goods)
    wx.navigateTo({url: '/sub/user/shop-order-list/work-order?id='+ workId})
    // this.$router.push({name: 'work-order', params: {workId: goods.workId}});
  },
  //检查商品售后状态
  goodsStatus(goods) {
    if (this.teamId != this.user.id) {
      return 0;
    }

    if (goods.status == 2 || goods.status == 3) {
      return 1;
    }

    if (goods.status == 5) {
      return 2;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})